package com.siyao.redvip.ui.adapter;

import java.util.List;

import com.siyao.redvip.R;
import com.siyao.redvip.bean.Comment;
import com.siyao.redvip.components.emojicon.EmoticonsTextView;
import com.siyao.redvip.components.view.MoreImageView;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CommentAdapter extends BaseAdapter{
	
	private List<Comment>  comments;
	private LayoutInflater mInflater;// 加载list_item
	private Context context;
	public CommentAdapter(Context context) {
		this.context = context;
		mInflater = LayoutInflater.from(context);
	}


	public void setComments(List<Comment> comments) {
		this.comments = comments;
	}

	@Override
	public int getCount() {
		return comments.isEmpty() ? 0 : comments.size();
	}

	@Override
	public Object getItem(int position) {
		return position < 0 ? 0 : position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if(convertView==null){
			viewHolder = new ViewHolder();
			convertView=mInflater.inflate(R.layout.item_comment, null);
			viewHolder.tv_username = (TextView) convertView.findViewById(R.id.tv_username);
			viewHolder.tv_time =  (TextView) convertView.findViewById(R.id.tv_time);
			viewHolder.tv_content =   (EmoticonsTextView) convertView.findViewById(R.id.tv_content);
			convertView.setTag(viewHolder);
		}else{
			viewHolder=(ViewHolder) convertView.getTag();
		}
		
		final Comment comment=comments.get(position);
		viewHolder.tv_username.setText(comment.getName());
		viewHolder.tv_time.setText(comment.getTime());
		viewHolder.tv_content.setText(comment.getContent());
		
		return convertView;
	}
	
	private static class ViewHolder {
		public TextView tv_username;
		public TextView tv_time;
		public EmoticonsTextView tv_content;

	}
	
	public void setDataChanged(List<Comment> mDatas) {
        if (mDatas != null && !mDatas.isEmpty()) {
            this.comments = mDatas;
            this.notifyDataSetChanged();
        }
    }

    public void clear() {
        if (comments != null && !comments.isEmpty()) {
            this.comments.clear();
        }
        this.notifyDataSetChanged();
    }
}
